import { UaCard } from "../../../models/ua-card.model";
import { GlobalService } from "../../../services/global.service";
import { UaCardService } from "../../../services/ua-card.service";

// pages/user/ap/ap.page.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cardList: [] as UaCard[],
    FILE_HOST: GlobalService.STATIC_ASSETS_HOST,
    hasMore: true,
    display: true,
    detailVisible: false,
    detailCard: {} as UaCard
  },

  customData: {
    cardList: [] as UaCard[],
    init: false,
    currentPage: 0,
    size: 18,
    totalPage: 0,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    wx.setNavigationBarTitle({
      title: 'AP展示'
    });
    UaCardService.ap().then((response: UaCard[]) => {
      this.customData.cardList = response;
      this.customData.totalPage = Math.ceil(this.customData.cardList.length / this.customData.size);
      this.setData({
        cardList: JSON.parse(JSON.stringify(this.customData.cardList.slice(0, this.customData.size))),
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.onLoadMore();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onRefresh() {
    this.customData.currentPage = 0;
    this.cutlist();
  },

  onLoadMore() {
    if (!this.data.hasMore) {
      return;
    }
    this.customData.currentPage++;
    this.cutlist();
  },

  cutlist() {
    const start = this.customData.currentPage * this.customData.size;
    const stop = start + this.customData.size;
    this.setData({
      cardList: JSON.parse(JSON.stringify(this.customData.cardList.slice(0, stop))),
      hasMore: stop < this.customData.cardList.length
    })
  },

  openDetail(e: any) {
    console.log('detail')
    for (const card of this.customData.cardList) {
      if (card.id === e.detail) {
        this.setData({
          detailVisible: true,
          detailCard: card
        });
        break;
      }
    }
  },

  onDetailVisibleChange(e: any) {
    this.setData({
      detailVisible: e.detail.visible,
    });
  },

  onPrev(e: any) {
    const detailCardId = e.detail;
    let p = 0;
    for (let i = 0; i < this.customData.cardList.length; i++) {
      if (detailCardId === this.customData.cardList[i].id) {
        p = i;
        break;
      }
    }
    if (p === 0) {
      p = this.customData.cardList.length - 1;
    } else {
      p--;
    }
    this.setData({
      detailCard: this.customData.cardList[p]
    })
  },

  onNext(e: any) {
    const detailCardId = e.detail;
    let p = 0;
    for (let i = 0; i < this.customData.cardList.length; i++) {
      if (detailCardId === this.customData.cardList[i].id) {
        p = i;
        break;
      }
    }
    if (p === this.customData.cardList.length - 1) {
      p = 0;
    } else {
      p++;
    }
    this.setData({
      detailCard: this.customData.cardList[p]
    })
  }
})